package com.dazzle.system.controller;

import java.util.HashSet;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dazzle.core.util.query.QueryFilter;
import com.dazzle.system.entity.SysResource;
import com.dazzle.system.entity.SysRole;
import com.dazzle.system.service.SysResourceService;
import com.dazzle.system.service.SysRoleService;

import flexjson.JSONSerializer;

@Controller
@RequestMapping("/system")
public class SysRoleController{
	@Resource
	private SysRoleService sysRoleService;
	
	@Resource
	private SysResourceService sysResourceService;
	
	@RequestMapping("/rolelist")
	public String list(HttpServletRequest request,HttpServletResponse response){
		QueryFilter queryFilter = new QueryFilter(request);
		List<SysRole> list = sysRoleService.getAll(queryFilter);
		request.setAttribute("roleList", list);
		request.setAttribute("pageBean", queryFilter.getPagingBean());
		List<SysResource> resAll = sysResourceService.getAll();
		request.setAttribute("resAll", resAll);
		return "system/sys_role_list";
	}
	@RequestMapping("/add_sys_role")
	public String add(@ModelAttribute("SysRoleForm") SysRole sysRole,HttpServletRequest request) {
		if(sysRole!=null){
			HashSet<SysResource> resources = new HashSet<SysResource>();
			String[] resIds = request.getParameterValues("resIds");
			if(resIds !=null && resIds.length>0){
				for(String resId:resIds){
					if(StringUtils.isNotEmpty(resId))
						resources.add(sysResourceService.get(new Long(resId)));
				}
			}
			sysRole.setResources(resources);
			sysRoleService.save(sysRole);
		}
		return "forward:rolelist.do";
	}
	@RequestMapping("/json_get_sys_role")
	public  String get(HttpServletRequest request,HttpServletResponse response) {
		String roleId = request.getParameter("editId");
		if(StringUtils.isNotEmpty(roleId)){
			SysRole sysRole = sysRoleService.get(new Long(roleId));
			JSONSerializer json = new JSONSerializer();
			request.setAttribute("jsonString", json.include("resources").serialize(sysRole));
		}
		return "jsonString";
	}
	
	@RequestMapping("/json_del_sys_role")
	public String del(HttpServletRequest request,HttpServletResponse response){
		String ids = request.getParameter("ids");
		StringBuffer msg = new StringBuffer("{'success':'true'");
		if(StringUtils.isNotEmpty(ids)){
			String[] roleIds = ids.split(",");
			msg.append(",'msg':'Role ");
			for(String roleId : roleIds){
				if(StringUtils.isNotEmpty(roleId)){
					SysRole readyToRemove = sysRoleService.get(new Long(roleId));
					msg.append(readyToRemove.getRoleName()).append(",");
					sysRoleService.remove(readyToRemove);
				}
			}
			msg.deleteCharAt(msg.length()-1);
			msg.append(" had been remove!'");
		}
		msg.append("}");
		request.setAttribute("jsonString", msg);
		return "jsonString";
	}
}